package com.hgd.tools;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;

import com.hgd.ORM.Admin;
import com.hgd.ORM.Device;
import com.hgd.ORM.DispatchCfg;
import com.hgd.ORM.Domain;
import com.hgd.ORM.ManagementStation;
import com.hgd.ORM.Task;
import com.hgd.ORM.TaskKind;
import com.hgd.XML.TurnXMLToConfiguration;
import com.hgd.XML.TurnXmlToDevice;
import com.hgd.XML.configurationXml;
import com.hgd.hibernate.HibernateUtil;
import java.util.Collections;

public class GetMinPayLoadDomain implements Comparator<Domain>{

	public int compare(Domain domain1, Domain domain2) {
		
		if (domain1 == null) {
			return 1;
		}
		if (domain2 == null || !(domain2 instanceof Domain)) {
			return -1;
		}
		long payLoad1 = domain1.getPayLoad();
		long payLoad2 = domain2.getPayLoad();
		
		return payLoad1 > payLoad2 ? 1 : payLoad1 < payLoad2 ? -1 : 0;
	}
	
	
	public static void main(String[] args) {
		Session session = HibernateUtil.getSesion();
		try {
			session.beginTransaction();		
			
			String hql = "select d from Domain d left join d.managementStation ms where ms.MSId=3";
			List<Domain> domains = session.createQuery(hql).list();
			
			Collections.sort(domains, new GetMinPayLoadDomain());
			for (Iterator iterator = domains.iterator(); iterator.hasNext();) {
				Domain domain = (Domain) iterator.next();
				System.out.println(domain.getPayLoad());
			}
			
			session.flush();
			session.getTransaction().commit();					
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
}
